package superdashboard;

import edu.wpi.first.wpilibj.networktables.NetworkTable;
import java.io.ByteArrayInputStream;
import java.io.DataInputStream;
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetSocketAddress;
import java.net.SocketAddress;
import org.newdawn.slick.AppGameContainer;
import org.newdawn.slick.BasicGame;
import org.newdawn.slick.Color;
import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.ImageBuffer;
import org.newdawn.slick.SlickException;

/**
 *
 * @author TJ
 */
public class SuperDashboard extends BasicGame {
	public static final String CRIO_IP = "10.6.75.2";
	public static final String NETBOOK_IP = "localhost";
	public static final int MY_PORT = 14000;
	public static final int NETBOOK_PORT = 15000;
    public static final String IDENTIFIER = "SuperDash_";
    
    private NetworkTable table;
    private DatagramSocket udpSocket;
    private SocketAddress netbookAddress;
    private DataReceiver receiver;
    private ImageBuffer buffer;
    
    /**
     * @param args
     * @throws IOException
     * @throws SlickException 
     */
    public static void main(String[] args) throws IOException, SlickException {
    	AppGameContainer container = new AppGameContainer(new SuperDashboard());
    	container.setDisplayMode(640, 480, false);
    	container.start();
    }

    public SuperDashboard() throws IOException {
    	super("Super Dashboard");
    	NetworkTable.setClientMode();
        NetworkTable.setIPAddress(CRIO_IP);
        table = NetworkTable.getTable("SmartDashboard");
                
        udpSocket = new DatagramSocket(MY_PORT);
        netbookAddress = new InetSocketAddress(NETBOOK_IP, NETBOOK_PORT);
        
        receiver = new DataReceiver(udpSocket);
        Thread thread = new Thread(receiver);
        thread.start();
        
        buffer = new ImageBuffer(640, 480);
    }

	@Override
	public void render(GameContainer arg0, Graphics arg1) throws SlickException {
		AppGameContainer container = (AppGameContainer) arg0;
		if((int) table.getNumber("Image Width", 640) != container.getWidth() || (int) table.getNumber("Image Height", 480) != container.getHeight()) {
			container.setDisplayMode((int) table.getNumber("Image Width", 640), (int) table.getNumber("Image Height", 480), false);
		}

		byte[][][] data = receiver.getData();
		System.out.println(data[0][0][0] + "," + data[0][0][1] + "," + data[0][0][2]);
		for (int a = 0; a < data.length -1; a+=1){
			for (int b = 0; b < data[0].length-1; b+=1 ){
				buffer.setRGBA(a, b, data[a][b][0] + 127,data[a][b][1] + 127,data[a][b][2]+127, 255);
			}
		}
		
		arg1.drawImage(buffer.getImage(), 0, 0);
	}

	@Override
	public void init(GameContainer arg0) throws SlickException {
	}

	@Override
	public void update(GameContainer arg0, int arg1) throws SlickException {
		
	}
}
